package com.qiall.project.service.addproject;


import com.alibaba.fastjson.JSON;
import com.qiall.common.request.GetByIdRequest;
import com.qiall.project.ability.request.AddProjectRequest;
import com.qiall.project.ability.request.ProjectResponse;
import com.qiall.project.data.entity.ProjectInfo;
import com.qiall.project.data.enums.PermissionTypeEnum;
import com.qiall.project.data.mapper.ProjectInfoMapper;
import com.qiall.user.ability.entity.OrganizationResponse;
import com.qiall.user.ability.entity.UserInfoDTO;
import com.qiall.user.ability.service.organization.OrganizationService;
import com.qiall.user.ability.service.user.UserService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class ProjectTrans {

    @Autowired
    private ProjectInfoMapper projectInfoMapper;
    @DubboReference
    private UserService userService;
    @DubboReference
    private OrganizationService organizationService;

    public ProjectInfo trans(AddProjectRequest projectRequest) {
        ProjectInfo projectInfo = new ProjectInfo();

        projectInfo.setParentId(projectRequest.getParentId());
        projectInfo.setId(projectRequest.getId());
        // build
        if (projectRequest.getParentId() != null) {
            ProjectInfo parentProject = projectInfoMapper.select(projectRequest.getParentId());
            String parentIds = "";
            if (StringUtils.isNotBlank(parentProject.getParentIds())) {
                parentIds = parentProject.getParentIds() + "," + parentProject.getId();
            } else {
                parentIds = parentProject.getId() + "";
            }
            projectInfo.setParentIds(parentIds);
        }
        projectInfo.setProjectName(projectRequest.getProjectName());
        projectInfo.setContext(projectRequest.getContext());
        PermissionTypeEnum permissionTypeEnum = PermissionTypeEnum.ofDesc(projectRequest.getPermission());
        projectInfo.setPermission(permissionTypeEnum.getCode());
        projectInfo.setStartTime(projectRequest.getStartTime());
        projectInfo.setEndTime(projectRequest.getEndTime());
        projectInfo.setProgressRate(projectRequest.getProgressRate());
        projectInfo.setProjectLeaderId(projectRequest.getProjectLeaderId());
        projectInfo.setProjectMemberIds(projectRequest.getProjectMemberIds());
        if (MapUtils.isNotEmpty(projectRequest.getAttributes())) {
            projectInfo.setAttributes(JSON.toJSONString(projectRequest.getAttributes()));
        }
        projectInfo.setCreatorId(projectRequest.getCreatorId());
        projectInfo.setOrgId(projectRequest.getOrgId());

        return projectInfo;
    }

    public ProjectResponse trans(ProjectInfo projectInfo) {
        ProjectResponse projectResponse = new ProjectResponse();
        projectResponse.setId(projectInfo.getId());
        projectResponse.setParentId(projectInfo.getParentId());
        if (projectInfo.getParentId() != null) {
            ProjectInfo parent = projectInfoMapper.select(projectInfo.getParentId());
            projectResponse.setParentProjectName(parent.getProjectName());
        }

        projectResponse.setParentIds(projectInfo.getParentIds());
        if (StringUtils.isNotBlank(projectInfo.getParentIds())) {
            String[] ids = projectInfo.getParentIds().split(",");
            String parentNames = "";
            for (String id : ids) {
                ProjectInfo projectInfoTmp = projectInfoMapper.select(Integer.parseInt(id));
                parentNames += projectInfoTmp.getProjectName() + ",";
            }
            projectResponse.setParentProjectNames(parentNames);
        }
        projectResponse.setProjectName(projectInfo.getProjectName());
        projectResponse.setContext(projectInfo.getContext());
       String desc= PermissionTypeEnum.ofCode(projectInfo.getPermission()).getDesc();
        projectResponse.setPermission(desc);
        projectResponse.setStartTime(projectInfo.getStartTime());
        projectResponse.setEndTime(projectInfo.getEndTime());
        projectResponse.setProgressRate(projectInfo.getProgressRate());
        projectResponse.setProjectLeaderId(projectInfo.getProjectLeaderId());
        UserInfoDTO leader = userService.getUserInfo(projectInfo.getProjectLeaderId()).getBody();
        projectResponse.setProjectLeaderName(leader.getUserName());
        projectResponse.setProjectMemberIds(projectInfo.getProjectMemberIds());
        if (StringUtils.isNotBlank(projectInfo.getProjectMemberIds())) {
            String[] ids = projectInfo.getProjectMemberIds().split(",");
            String names = "";
            for (String id : ids) {
                UserInfoDTO userInfoDTO = userService.getUserInfo(Integer.parseInt(id)).getBody();
                names += userInfoDTO.getAccount() + ",";
            }
            projectResponse.setProjectMemberNames(names);

        }
        if (StringUtils.isNotBlank(projectInfo.getAttributes())) {
            projectResponse.setAttributes(JSON.parseObject(projectInfo.getAttributes(), HashMap.class));
        }
        projectResponse.setCreatorId(projectInfo.getCreatorId());
        UserInfoDTO creator = userService.getUserInfo(projectInfo.getCreatorId()).getBody();
        projectResponse.setCreatorName(creator.getAccount());
        projectResponse.setOrgId(projectInfo.getOrgId());
        GetByIdRequest getByIdRequest = new GetByIdRequest();
        getByIdRequest.setId(projectInfo.getOrgId());
        OrganizationResponse organizationResponse = organizationService.getOrganizationInfo(getByIdRequest).getBody();
        projectResponse.setOrgName(organizationResponse.getOrgName());
        return projectResponse;

    }
}
